package net.huashitong.ssydt.provider.article.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.huashitong.ssydt.PaginationResult;
import net.huashitong.ssydt.article.entity.SsArticleType;
import net.huashitong.ssydt.article.entity.vo.ArticleTypeInVO;
import net.huashitong.ssydt.article.entity.vo.ArticleTypeQueryInVO;
import net.huashitong.ssydt.provider.article.service.SsArticleTypeService;
import net.huashitong.ssydt.provider.web.model.Result;
import net.huashitong.ssydt.provider.web.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;

/**
 * 文章类型接口（后台）
 *
 * @author hemj
 * @version 2017/9/14
 */
@Api(value = "/ssydt/article/type", tags = {"文章类型接口（后台）"})
@RestController
@RequestMapping("/ssydt/article/type")
public class SsArticleTypeController {

	private SsArticleTypeService ssArticleTypeService;

	@Autowired
	public SsArticleTypeController(SsArticleTypeService ssArticleTypeService) {
		this.ssArticleTypeService = ssArticleTypeService;
	}

	@ApiOperation(value = "可用文章类型列表", notes = "可用文章类型列表")
	@GetMapping("/validlist")
	public Result<ArrayList<SsArticleType>> listValidArticleTypes() {
		return ResultUtils.getSuccessResults((ArrayList<SsArticleType>) ssArticleTypeService.listValidArticleTypes());
	}

	@ApiOperation(value = "文章类型分页查询", notes = "文章类型分页查询")
	@GetMapping
	public Result<PaginationResult<SsArticleType>> paginationQuery(@ModelAttribute ArticleTypeQueryInVO articleTypeQueryInVO) {
		return ResultUtils.getSuccessResults(ssArticleTypeService.paginationQuery(articleTypeQueryInVO));
	}

	@ApiOperation(value = "加载文章类型信息", notes = "加载文章类型信息")
	@GetMapping("/{typeId}")
	public Result<SsArticleType> get(@PathVariable Long typeId) {
		return ResultUtils.getSuccessResults(ssArticleTypeService.getById(typeId));
	}

	@ApiOperation(value = "新增文章类型", notes = "新增文章类型")
	@PostMapping
	public Result<String> post(@Valid @RequestBody ArticleTypeInVO articleTypeInVO) {
		ssArticleTypeService.insert(articleTypeInVO);
		return ResultUtils.getSuccessResults();
	}

	@ApiOperation(value = "修改文章类型", notes = "修改文章类型")
	@PutMapping("/{typeId}")
	public Result<String> put(@PathVariable Long typeId, @Valid @RequestBody ArticleTypeInVO articleTypeInVO) {
		articleTypeInVO.setId(typeId);
		ssArticleTypeService.update(articleTypeInVO);
		return ResultUtils.getSuccessResults();
	}

}
